package com.zy.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.zy.constant.MessageConstant;
import com.zy.entity.PageResult;
import com.zy.entity.QueryPageBean;
import com.zy.entity.Result;
import com.zy.pojo.CheckGroup;
import com.zy.pojo.CheckItem;
import com.zy.service.CheckGroupService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {

	@Reference
	private CheckGroupService checkGroupService;
	@PreAuthorize("hasAuthority('CHECKGROUP_ADD')")
	@RequestMapping("/add")
	public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {

		try {
			checkGroupService.add(checkGroup,checkitemIds);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, MessageConstant.ADD_CHECKGROUP_SUCCESS);
		}
		return new Result(true, MessageConstant.ADD_CHECKGROUP_FAIL);
	}

	@RequestMapping("/findPage")
	public PageResult findPage(@RequestBody QueryPageBean queryBean){
		return checkGroupService.findPage(queryBean);
	}

	@PreAuthorize("hasAuthority('CHECKGROUP_DELETE')")
	@RequestMapping("/delete")
	public Result delete(Integer id){
		try {
			checkGroupService.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
		}
		return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
	}


	@RequestMapping("/findCheckitemAndCheckgroupId")
	public Result findCheckitemAndCheckgroupId(Integer id){
		Result successRes = new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS);
		try {
			List<Integer> checkItemList = checkGroupService.findCheckitemAndCheckgroupId(id);
			successRes.setData(checkItemList);
//			System.out.println(checkItemList);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
		}
		return successRes;
	}

	@PreAuthorize("hasAuthority('CHECKGROUP_EDIT')")
	@RequestMapping("/edit")
	public Result edit(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
		try {
			checkGroupService.edit(checkitemIds,checkGroup);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
		}
		return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
	}

	@RequestMapping("/findAll")
	public Result finAll(){
		try {
			List<CheckGroup> checkGroups =checkGroupService.finAll();
			return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroups);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
		}
	}

	@RequestMapping("/findAllOrder")
	public Result findAllOrder(Integer id){
		try {
			List<CheckGroup> list =checkGroupService.findAllOrder(id);
			return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
		}

	}


}
